<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\User;
use App\Models\Product;
use App\Models\Record;
use App\Http\Controllers\Common\SmsController;
use App\Mail\NotifyAdmin;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Response;
use App\Models\Region;
use App\Models\Card_apply;
use App\Models\Card;

class ApplyController extends Controller
{
    public function __construct()
    {
        $this->middleware(function ($request, $next) {
            if (session('userid')) {
                $user = User::find(session('userid'));
            } else {
                $user = '';
            }
            $data['user'] = $user;
            view()->share($data);
            return $next($request);
        });
    }

    //快速申请提交
    public function FastApply(Request $request)
    {
        $name = $request->get('name');
        $fee = $request->get('fee');
        $sex = $request->get('sex');
        $mobile = $request->get('mobile');
        $timeindex = $request->get('time_limit');
        $time_limit = config('front.time_limit')[$timeindex];
        $code = $request->get('code');
        if (session('userid')) {
            $userid = session('userid');
        } else {
            $userid = 0;
        }

        $verify = SmsController::verifyCode($mobile, $code);

        if ($verify['code'] == '0000') {
            $data = [
                'user_id' => $userid,
                'name' => $name,
                'origin' => 'FAST',
                'mobile' => $mobile,
                'fee' => $fee,
                'sex' => $sex,
                'time_limit' => $time_limit,
                'status' => 0,
                'create_time' => date('Y-m-d H:i:s', time())
            ];
            //return Response::json($data);
            $result = Record::create($data);
            if ($result) {
                //成功短信通知管理员
                //$smsService_notify=new CCSM1SService();
                //$smsService_notify->sendNotify(config('HomeConfig.first_admin'),$name,$mobile,'贷款申请');
                //成功邮件通知管理员
                $email = config('front.mail');
                try {
                    Mail::to($email)->send(new NotifyAdmin($result, $request));
                } catch (\Exception $e) {
                    return Response::json(['code' => '0000', 'message' => '资料提交失败，请稍后重试']);
                }
                //var_dump($co)'exit;
                return Response::json(['code' => '0000', 'message' => '资料提交成功，请等待贷款顾问联系']);
            } else {
                return Response::json(['code' => '0000', 'message' => '资料提交失败，请稍后重试']);
            }
        } else {
            return Response::json($verify, JSON_UNESCAPED_UNICODE);
        }
    }


    //信用卡申请 第一步
    public function cardstepone(Request $request)
    {
        //dd($_SERVER['HTTP_HOST']);
        $provinces = Region::where('p_id', 0)->get();
        //return $provinces;
        if ($request->ajax()) {
            $data['name'] = $request->get('name');
            $data['mobile'] = $request->get('mobile');
            $data['sex'] = $request->get('sex');
            $data['user_id'] = session('userid');
            $province = Region::where('region_code', $request->get('province'))->first();
            $city = Region::where('region_code', $request->get('city'))->first();
            $data['city'] = $province->region_name . $city->region_name;
            $data['status'] = -2;
            $data['create_time'] = date('Y-m-d H:i:s', time());
            $result = Card_apply::create($data);
            if ($result) {
                return Response::json(['code' => '0000', 'message' => '资料提交成功', 'recordid' => $result['id']]);
            } else {
                return Response::json(['code' => '0001', 'message' => '资料提交失败']);
            }
        }
        return view('home.apply.cardstepone', compact('provinces'));
    }

    //信用卡申请 第二步
    public function cardsteptwo(Request $request)
    {
        $config = config('HomeConfig');
        $recordid = $request->get('recordid');
        if ($request->ajax()) {
            $recordid = $request->get('recordid');
            $apply = Card_apply::find($recordid);
            $data['age'] = $request->get('age');
            $data['job'] = $request->get('job');
            $data['salary'] = $request->get('salary');
            $data['is_house'] = $request->get('is_house');
            $data['is_car'] = $request->get('is_car');
            $data['is_social'] = $request->get('is_social');

//            $indexjob=$request->get('job');
//            $data['job']=config('HomeConfig.job')[$indexjob];

//            $indexhouse=$request->get('is_house');
//            $data['is_house']=config('HomeConfig.is_house')[$indexhouse];

//            $indexcar=$request->get('is_car');
//            $data['is_car']=config('HomeConfig.is_car')[$indexcar];

//            $indexsocial=$request->get('is_social');
//            $data['is_social']=config('HomeConfig.is_social')[$indexsocial];

            $data['is_fund'] = $request->get('is_fund');
            $data['fee'] = $request->get('fee');
            $data['status'] = -1;
            $data['create_time'] = date('Y-m-d H:i:s', time());
            $result = $apply->where('id', $recordid)->update($data);
            if ($result) {
                return Response::json(['code' => '0000', 'message' => '资料提交成功', 'recordid' => $recordid]);
            } else {
                return Response::json(['code' => '0001', 'message' => '资料提交失败']);
            }
        }
        return view('home.apply.cardsteptwo', compact('recordid', 'config'));
    }

    //信用卡申请 第三步
    public function cardstepthree(Request $request)
    {
        $cards = Card::get();
        $recordid = $request->get('recordid');
        if ($request->ajax()) {
            $recordid = $request->get('recordid');
            $apply = Card_apply::find($recordid);
            $card = Card::find($request->get("cardid"));
            $data['card_id'] = $request->get("cardid");
            $data['status'] = 0;
            $data['create_time'] = date('Y-m-d H:i:s', time());
            $result = $apply->where('id', $recordid)->update($data);
            if ($result) {
                return Response::json(['code' => '0000', 'message' => '资料提交成功', 'cardurl' => $card->url]);
            } else {
                return Response::json(['code' => '0001', 'message' => '资料提交失败']);
            }
        }
        return view('home.apply.cardstepthree', compact('recordid', 'cards'));
    }

    //具体贷款项申请提交第一步
    public function onestep(Request $request)
    {
        $product_id = $request->get('productid');
        if ($request->ajax()) {
            $data['name'] = $request->get('name');
            $data['mobile'] = $request->get('mobile');
            $data['origin'] = 'DETAIL';
            $data['user_id'] = session('userid');
            $data['product_id'] = $request->get('productid');

            $code = $request->get('code');

            $veifymobile = SmsController::verifyCode($data['mobile'], $code);

            if ($veifymobile['code'] != '0000') {
                return Response::json(['code' => $veifymobile['code'], 'message' => $veifymobile['message']]);
            }
            $data['status'] = -2;
            $data['create_time'] = date('Y-m-d H:i:s', time());
            $result = Record::create($data);
            if ($result) {
                return Response::json(['code' => '0000', 'message' => '资料提交成功', 'recordid' => $result['id']]);
            } else {
                return Response::json(['code' => '0001', 'message' => '资料提交失败']);
            }
        }
        return view('home.apply.onestep', compact('product_id'));
    }

    //具体贷款项申请提交第二步
    public function twostep(Request $request)
    {
        $config = config('front');
        $recordid = $request->get('recordid');
        if ($request->ajax()) {
            $recordid = $request->get('recordid');
            $record = Record::find($recordid);
            $data['fee'] = $request->get('fee');

            $indextime = $request->get('time_limit');
            $data['time_limit'] = config('front.time_limit')[$indextime];
            $data['city'] = $request->get('city');

            $indexjob = $request->get('job');
            $data['job'] = config('front.job')[$indexjob];

            $indexSalaryType = $request->get('salary_type');
            $data['salary_type'] = config('front.salary_type')[$indexSalaryType];
            $data['salary'] = $request->get('salary');

            $indexsocial = $request->get('is_social');
            $data['is_social'] = config('front.is_social')[$indexsocial];

            $indexcreadit = $request->get('credit');
            $data['credit'] = config('front.credit')[$indexcreadit];
            $data['status'] = -1;
            $data['create_time'] = date('Y-m-d H:i:s', time());

            $result = $record->where('id', $recordid)->update($data);
            if ($result) {
                return Response::json(['code' => '0000', 'message' => '资料提交成功']);
            } else {
                return Response::json(['code' => '0001', 'message' => '资料提交失败']);
            }
        }
        return view('home.apply.twostep', compact('config', 'recordid'));
    }

    //具体贷款项申请提交第三步
    public function threestep(Request $request)
    {
        $config = config('front');
        $recordid = $request->get('recordid');
        if ($request->ajax()) {
            $recordid = $request->get('recordid');
            $record = Record::find($recordid);
            $data['age'] = $request->get('age');
            $data['sex'] = $request->get('sex');
            $data['register'] = $request->get('register');

            $indexinsure = $request->get('is_insurance');
            $data['is_insurance'] = config('front.is_insurance')[$indexinsure];

            $indexhouse = $request->get('is_house');
            $data['is_house'] = config('front.is_house')[$indexhouse];

            $indexcar = $request->get('is_car');
            $data['is_car'] = config('front.is_car')[$indexcar];
            $data['status'] = 0;
            $data['create_time'] = date('Y-m-d H:i:s', time());
            $result = $record->where('id', $recordid)->update($data);
            if ($result) {
                //成功短信通知管理员
                //$smsService_notify = new CCSM1SService();
                //$smsService_notify->sendNotify(config('HomeConfig.first_admin'), $record->name, $record->mobile, '贷款申请');
                //成功邮件通知管理员
                $email = config('front.mail');
                Mail::to($email)->send(new NotifyAdmin($record, $request));

                return Response::json(['code' => '0000', 'message' => '资料提交成功']);
            } else {
                return Response::json(['code' => '0001', 'message' => '资料提交失败']);
            }
        }
        return view('home.apply.threestep', compact('config', 'recordid'));
    }

    public function fourstep()
    {
        return view('home.apply.fourstep');
    }

}
